$(function() {
	if (window.errorMsg) {
		$("#alert-warning").append(window.errorMsg).removeClass("hidden").hide().fadeIn("slow");
		$("#alert-warning").fadeOut(5000);
	}

	var table = $('#meter').DataTable({
		"processing" : true,
		"serverSide" : true,
		"dom" : 'tr<"bottom"ip>',
		"ajax" : {
			"url" : window.basePath + "meter/list",
			"type" : "POST",
		},
		"language" : {
			"zeroRecords" : "无数据！",
			"infoEmpty" : "没有找到该条记录！",
			"infoFiltered" : "(共 _MAX_ 条记录)",
			"processing" : "加载中...",
			"lengthMenu" : "_MENU_ Einträge anzeigen",
			"info" : "显示第_START_ 到第 _END_条记录 （共 _TOTAL_ 条记录）",
			"infoPostFix" : "",
			"paginate" : {
				"first" : "第一页",
				"previous" : "上一页",
				"next" : "下一页",
				"last" : "最后一页"
			}
		},
		"columns" : [ {
			"class" : 'details-control',
			"orderable" : false,
			"data" : null,
			"defaultContent" : ''
		}, {
			"data" : "name"
		}, {
			"data" : "deviceKey"
		}, {
			"data" : "addr1"
		}, ],
		"order" : [ [ 1, "asc" ] ]

	});
	$.fn.dataTable.ext.errMode = 'throw';
	$("#searchName").on('keyup', function() {
		table.column(0).search(this.value).draw();
	});
	$("#searchDeviceKey").on('keyup', function() {
		table.column(1).search(this.value).draw();
	});
	$("#searchAddr1").on('keyup', function() {
		table.column(2).search(this.value).draw();
	});

	$('#meter tbody').on('click', 'td.details-control', function() {
		var tr = $(this).parents('tr');
		var row = table.row(tr);

		if (row.child.isShown()) {
			row.child.hide();
			tr.removeClass('shown');
		} else {
			row.child(format(row.data())).show();
			tr.addClass('shown');
		}
	});

	function format(data) {
		return '<table  class="table"><tr><td>累计热能：</td><td><em>' + Math.round(data.allheat * 100) / 100.0
				+ ' kWh</em></td><td>累计流量：</td><td><em>' + Math.round(data.allflow * 100) / 100.0
				+ ' m<SUP>3</SUP</em></td><td>瞬时热量：</td><td><em>' + data.currentHeat
				+ ' kW</em></td><td>瞬时流量：</td><td><em>' + Math.round(data.currentflow * 100) / 100.0
				+ ' m<SUP>3</SUP>/h</em></td></tr><tr><td>上个分摊周期热量:</td><td><em>' + Math.round(data.lastheat * 100)
				/ 100.0 + ' kW</em></td>' + '<td>进水温度：</td><td><em>' + Math.round(data.inTemperature * 10) / 10.0
				+ ' ℃</em></td><td>回水温度：</td><td><em>' + Math.round(data.outTemperature * 10) / 10.0
				+ ' ℃</em><td>通讯状态：</td><td><em>' + data.status + '</em></td></tr></table>';
	}

	$('#collapseOne').on('show.bs.collapse', function() {
		$("#arrow").removeClass("glyphicon glyphicon-chevron-down").addClass("glyphicon glyphicon-chevron-up");
	});
	$('#collapseOne').on('hide.bs.collapse', function() {
		$("#arrow").removeClass("glyphicon glyphicon-chevron-up").addClass("glyphicon glyphicon-chevron-down");
	});

});